<template>
  <section>
    <el-form label-width="100px">
      <!--<el-form-item label="拷贝问卷">-->
      <!--<el-select clearable v-model="selQuestion" :filterable="true" remote-->
      <!--:reserve-keyword="false" default-first-option  placeholder="请输入关键词"-->
      <!--:remote-method="remoteMethod" size="mini" value-key="id">-->
      <!--<el-option v-for="item of questionnaireList" :key="item.id" :label="item.title" :value="item"></el-option>-->
      <!--</el-select>-->
      <!--</el-form-item>-->
      <el-form-item label="问卷标题">
        <el-input type="text" v-model="currQa.title" size="mini" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item label="题目类型">
        <el-select v-model="currQa.type" size="mini">
          <el-option v-for="type of questionTypes" :key="type.id" :value="type.id" :label="type.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="currQa.type === 2" label="选择策略">
        <el-select v-model="currQa.tacticsId" size="mini ">
          <el-option v-for="tactics of tacticsList" :key="tactics.id" :value="tactics.id" :label="tactics.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item align="right">
        <el-button @click.native="btnCancel" size="mini">取消</el-button>
        <el-button @click.native="btnCreate" type="primary" size="mini">提交</el-button>
      </el-form-item>
    </el-form>
  </section>
</template>

<script>
  import errHandler from '@/assets/errHandler.js'
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  import Vue from 'vue'//后续添加属性，需要vue联动
  export default {
    name: "newQa",
    props:['currQa'],
    data(){
      return{
        questionTypes:[],
        tacticsList:[],
        questionnaireList:[]
      }
    },
    computed: {
      ...mapGetters(['department']),
    },
    created(){
      // this.getQaList()
      this.getQaTypeList()
      this.getTacticsList()
    },
    methods:{
      ...mapActions({
        fetch: 'fetch'
      }),
      remoteMethod(key) {
        if(key === '' || !key){//关键词为空或没有输入
          this.questionnaireList=[]
          return
        }
        this.fetch({name:"getQuestionnaireList",data:{key}})
          .then(res=>{
            console.log('搜索的问卷列表',res)
            this.questionnaireList=res.content
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      getQaTypeList(){
        this.fetch({name:"getQaTypeList"})
          .then(res=>{
            console.log(res)
            this.questionTypes=res
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      getTacticsList(){
        this.fetch({
          name:"getTacticsList"
        }).then(res=>{
          console.log('tacticsList',res)
          this.tacticsList = res
        }).catch(err=>{
          console.log(err)
          errHandler.handle(this,err)
        })
      },
      btnCancel(){
        this.$emit('cancel')
      },
      btnCreate(){
        //创建问卷
        if(!this.currQa.title){
          this.$message({type:'error',message:'标题不能为空'});
        }else if(!this.currQa.type){
          this.$message({type:'error',message:'请选择题目类型'});
        }else{
          let params = {title: this.currQa.title,typeId: this.currQa.type,companyId:this.department.companyId}
          if(this.currQa.type === 2){
            // params.isTactics = true
            params.tacticsId = this.currQa.tacticsId
          }
          if(this.selQuestion){//拷贝题目 这里的selQuestion 就是id号 copyQuestionnaire
            this.fetch({name:'copyQuestionnaire',data:{
                id:this.selQuestion,
                ...params,
              }}).then(res=>{
              if(!res.errCode){
                this.$message({type:'success',message:'拷贝问卷成功'});
                this.$router.push('/qa/edit/'+res.id);
              }else{
                this.$message({type:'error',message:res.errMsg});
              }
            }).catch(error=>{
              console.log(error);
              this.$message({type:'error',message:'服务器出错'});
            })
          }else{
            console.log(params)
            this.fetch({name:"createQuestionnaire",data:{...params}})
              .then(res=>{
                if(!res.errCode){
                  this.$message({type:'success',message:'新建问卷成功'});
                  this.$router.push('/qa/edit/'+res.id);
                }else{
                  this.$message({type:'error',message:res.errMsg});
                }
              })
              .catch(err=>{
                console.log(err)
                errHandler.handle(this,err)
              })
          }
        }
      },
    }
  }
</script>

<style scoped>

</style>
